package com.itcast.flink.connectors.kafka;

import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;

import java.util.Properties;

/**
 * @program: flink-app
 * @description: kafka的队列输入到flink中
 * @author: zhanghz001
 * @create: 2021-07-23 17:15
 **/
public class ZhzKafkaSourceApplication {
    public static void main(String[] args) throws Exception {
        //    创建运行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        
        //设置kafka服务连接信息
        Properties properties = new Properties();
        properties.setProperty("bootstrap.servers", "192.168.23.140:9092");
        properties.setProperty("group.id", "fink_group");
        
        //创建kafka消费端
        FlinkKafkaConsumer<String> kafkaConsumer = new FlinkKafkaConsumer<>(
                //目标topic
                "flink-source",
                // 序列化配置
                new SimpleStringSchema(),
                properties
        );
        // kafkaConsumer.setStartFromEarliest(); // 尽可能从最早的记录开始
        // kafkaConsumer.setStartFromLatest(); // 从最新的记录开始
        // kafkaConsumer.setStartFromTimestamp(...); // 从指定的时间开始（毫秒）
        // kafkaConsumer.setStartFromGroupOffsets(); // 默认的方法
        
        //读取kafka数据源
        DataStreamSource<String> socketStr = env.addSource(kafkaConsumer);
        
        socketStr.print().setParallelism(1);
        
        //执行任务
        env.execute("kafka to flink job");
    }
}
